import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../routes/routes.dart';
import '../../utils/alert_utils.dart';

class ChangePasswordController extends GetxController {
  TextEditingController newController = TextEditingController();
  TextEditingController confirmController = TextEditingController();
  bool canSubmit = false; // 是否能提交

  void submitClick(BuildContext context) async {
    if (!canSubmit) return;
    if (newController.text != confirmController.text) {
      return EasyLoading.showToast('密码不一致');
    }
    AlertUtils().showCenterTipsDialog(context, (e) {
      changePs();
    }, '是否修改密码', '修改后会退出当前账号');
  }

  void changePs() async {
    var result = await RequestManager().post(API.changePassword, params: {
      'password': newController.text,
      'recheck_password': confirmController.text
    });
    if (result != null && result['code'] == 0) {
      EasyLoading.showToast('修改成功');
      SpUtil.cleanUserInfo();
      Get.offAllNamed(Routes.smsCodeLoginPage);
    }
  }

  void changeBtnStatus() {
    if (newController.text.isEmpty || confirmController.text.isEmpty) {
      canSubmit = false;
    } else if (newController.text.length < 6) {
      canSubmit = false;
    } else if (confirmController.text.length < 6) {
      canSubmit = false;
    } else {
      canSubmit = true;
    }
    update();
    // return true;
    // update();
  }
}
